package com.jinpeng.websocket.websocket;

import com.jinpeng.websocket.config.WebSocketServer;
import com.jinpeng.websocket.websocket.util.ApiReturnUtil;
import lombok.extern.slf4j.Slf4j;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import java.io.IOException;

/**
 * @ProjectName: websocket
 * @Package: com.jinpeng.websocket.websocket
 * @ClassName: SocketController
 * @Description: 消息控制器
 * @Author: jp
 * @CreateDate: 2019/12/10 10:35
 * @UpdateUser: 更新者
 * @UpdateDate: 2019/12/10 10:35
 * @UpdateRemark: 更新说明
 * @Version: 1.0
 */
@Controller
@RequestMapping("socket")
@Slf4j
public class SocketController {


    /**
     * 页面跳转
     * @return
     */
    @RequestMapping("index")
    public String ftlIndex() {
        return "socket.html";
    }



    /**
     * 开启推送推送数据接口
     */
    @ResponseBody
    @RequestMapping("push")
    public ApiReturnUtil pushToWeb(String sender,String recipient,String message) {
        WebSocketServer.sendInfo(message,sender,recipient);
        return ApiReturnUtil.success(sender);
    }



    /**
     * 开启推送推送数据接口
     */
    @ResponseBody
    @RequestMapping("pushToCid")
    public ApiReturnUtil pushToCid(String sender,String recipient,String message) {
        WebSocketServer.sendInfo(message,sender,recipient);
        return ApiReturnUtil.success(sender);
    }


    /**
     * 主动关闭连接
     */
    @ResponseBody
    @RequestMapping("close")
    public ApiReturnUtil webSocketOff(String sender) {
        WebSocketServer.closeUserSocket(sender);
        log.info("==========关闭连接===================");
        return ApiReturnUtil.success("你的连接已关闭");
    }

}
